﻿// ***********************************************************************
// Project			: Micua
// Assembly         : Micua.Core
// Author           : iceStone
// Created          : 2013-11-23 14:25
//
// Last Modified By : iceStone
// Last Modified On : 2013-11-23 14:27
// ***********************************************************************
// <copyright file="Setting.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary>网站设置信息类</summary>
// ***********************************************************************

using System.Collections.Generic;
using Micua.Utility;

namespace Micua.Core.Configuration
{
    /// <summary>
    /// 网站设置信息类
    /// </summary>
    /// <remarks>
    ///  2013-11-23 14:27 Created By iceStone
    /// </remarks>
    public static partial class Setting
    {
        /// <summary>
        /// 设置信息字典(数据仓储)
        /// </summary>
        public static IDictionary<string, string> Settings { get; private set; }

        static Setting()
        {
            Settings = new Dictionary<string, string>();
        }

        #region 初始化设置信息字典 +static void Init(IDictionary<string, string> dict)
        /// <summary>
        /// 初始化设置信息字典
        /// </summary>
        /// <param name="dict">设置信息字典</param>
        public static void Init(IDictionary<string, string> dict)
        {
            //Settings = dict;
            foreach (var item in dict)
            {
                Settings.Add(item.Key.ToLower(), item.Value);
            }
        }
        #endregion

        #region 获取泛型类型的设置信息（JSON格式） +static T Get<T>(string key, T def = null) where T : class
        /// <summary>
        /// 获取泛型类型的设置信息（JSON格式）
        /// </summary>
        /// <remarks>
        ///  2013-11-23 14:27 Created By iceStone
        /// </remarks>
        /// <typeparam name="T">设置类型</typeparam>
        /// <param name="key">设置键</param>
        /// <param name="def">缺省值</param>
        /// <returns>制定类型的设置信息</returns>
        public static T Get<T>(string key, T def = null) where T : class
        {
            var json = GetString(key);
            try
            {
                return JsonHelper.Deserialize<T>(json);
            }
            catch (System.Exception)
            {
                return def;
            }
        }
        #endregion

        #region 获取String类型设置信息 +static string GetString(string key, string def = "")
        /// <summary>
        /// 获取字符串类型设置信息
        /// </summary>
        /// <remarks>
        ///  2013-11-23 14:27 Created By iceStone
        /// </remarks>
        /// <param name="key">设置键</param>
        /// <param name="def">缺省值</param>
        /// <returns>设置值</returns>
        public static string GetString(string key, string def = "")
        {
            if (Settings != null && Settings.ContainsKey(key))
                return Settings[key];
            return def;

        }
        #endregion

        #region 修改信息字典值 +static void Set(string key, string value)
        /// <summary>
        /// 修改信息字典值
        /// </summary>
        /// <remarks>
        ///  2013-11-23 14:27 Created By iceStone
        /// </remarks>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void Set(string key, string value)
        {
            lock ("SettingLockHelper")
            {
                if (Settings.ContainsKey(key))
                    Settings[key] = value;
                else
                    Settings.Add(key, value);
            }
        }
        #endregion

        #region 移除单个设置 +static void Remove(string key)
        /// <summary>
        /// 移除单个设置
        /// </summary>
        /// <remarks>
        ///  2013-11-23 14:27 Created By iceStone
        /// </remarks>
        /// <param name="key">键</param>
        public static void Remove(string key)
        {
            lock ("SettingLockHelper")
            {
                Settings.Remove(key);
            }
        }
        #endregion

        #region 移除多个设置 +static void Remove(params string[] keys)
        /// <summary>
        /// 移除多个设置
        /// </summary>
        /// <remarks>
        ///  2013-11-23 14:27 Created By iceStone
        /// </remarks>
        /// <param name="keys">键</param>
        public static void Remove(params string[] keys)
        {
            lock ("SettingLockHelper")
            {
                foreach (var key in keys)
                {
                    Settings.Remove(key);
                }
            }
        }
        #endregion

        #region 获取主题设置项的私有Key +static string GetThemeOptionKey(string name)
        /// <summary>
        /// 获取主题设置项的私有Key
        /// </summary>
        /// <remarks>
        ///  2013-11-23 14:27 Created By iceStone
        /// </remarks>
        /// <param name="name">选项名称</param>
        /// <returns>选项私有Key</returns>
        public static string GetThemeOptionKey(string name)
        {
            return string.Format("_{0}_{1}", "", name);
        }
        #endregion
    }

    #region bak
    ///// <summary>
    ///// 网站设置信息类
    ///// </summary>
    ///// <remarks>
    /////  2013-11-23 14:27 Created By iceStone
    ///// </remarks>
    //public static class Setting
    //{

    //    /// <summary>
    //    /// 设置信息字典
    //    /// </summary>
    //    private static IDictionary<string, object> _dict;

    //    private static SettingDictionary _dynamicSettingDictionary;
    //    /// <summary>
    //    /// 设置包
    //    /// </summary>
    //    public static dynamic SettingBag
    //    {
    //        get
    //        {
    //            return _dynamicSettingDictionary ?? (_dynamicSettingDictionary = new SettingDictionary(() => _dict));
    //        }
    //    }

    //    public static void InitSettingDict(IDictionary<string, object> setting)
    //    {
    //        _dict = setting;
    //    }

    //}

    ///// <summary>
    ///// 
    ///// </summary>
    //public class SettingDictionary : DynamicObject
    //{
    //    private Func<IDictionary<string, object>> _settingData;
    //    /// <summary>
    //    /// 
    //    /// </summary>
    //    /// <param name="settingData"></param>
    //    public SettingDictionary(Func<IDictionary<string, object>> settingData)
    //    {
    //        _settingData = settingData;
    //    }
    //}
    #endregion
}
